﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace MVC.Models {
  public class SqlStudentRepository :IStudentRepository{
    private readonly PCDbContext _context;

    public SqlStudentRepository(PCDbContext context) {
      _context = context;
    }

    public Student AddStudent(Student student) {
      _context.Students.Add(student);
      _context.SaveChanges();
      return student;
    }

    public Student DeleteStudent(int id) {
      Student student = _context.Students.Find(id);
      if (student != null) {
        _context.Students.Remove(student);
        _context.SaveChanges();
      }
      return student;
    }

    public Student GetStudent(int id) {
      return _context.Students.Find(id);
    }

    public IEnumerable<Student> GetStudents() {
      return _context.Students;
    }

    public Student UpdateStudent(Student updateStudent) {
      var student = _context.Students.Attach(updateStudent);
      student.State = Microsoft.EntityFrameworkCore.EntityState.Modified;
      _context.SaveChanges();
      return updateStudent;
    }
  }
}
